clear
set more off

cap log close
log using $logdir/takeup_by_ppp.log, replace


use $fromsasdir/result, clear
gen any_ertc = ertc > 0
gen any_amend = ertc_amendment > 0

destring ppp_first_ym, replace force
gen ppp_month = mod(ppp_first_ym,100)
gen ppp_year = floor(ppp_first_ym/100)
gen ppp_q = ceil(ppp_month/3)

gen ppp_date = yq(ppp_year,ppp_q)


gen year = floor(tp/100)
gen q = mod(tp,100)/3
gen date = yq(year,q)
format date %tq

gen has_ppp = ppp_date > 0 & ppp_date <= date

collapse any_ertc any_amend (sum) n_ertc = any_ertc n_amend = any_amend, by(date has_ppp) fast

qui sum n_ertc
assert r(min) >= 10

qui sum n_amend
assert r(min) >= 10


local any_ertcstr "On original returns"
local any_amendstr "On amended returns"

foreach var in any_ertc any_amend {
	twoway connected `var' date if has_ppp == 1 || ///
		connected `var' date if has_ppp == 0, lpattern("--") ///
			msymbol(circle_hollow) ///
			legend(order(1 "Observed to have PPP loan" 2 "All others")) ///
			scheme(s1color) xtitle("Date") ytitle("Share taking up ERC")
			
	graph export $sdodir/`var'_by_ppp.pdf, replace
	
}


qui save $outdir/takeup_by_ppp, replace


cap log close


